

jQuery(function ($) {
    //点击资讯和字画问答切换
    $('.information span').click(function () {
        var index = $(this).index();
        $(this).addClass('red').siblings().removeClass('red');
        $('.question').eq(index).addClass("active").siblings().removeClass("active");
    });

    //咨询、字画问答切换
    $('.information span').click(function(){
        var index = $(this).index();
        $(this).addClass("ask_color").siblings().removeClass("ask_color");
        $(".information_answer").eq(index).addClass('open').siblings().removeClass("open");;
    });

    //右侧导航栏
    //选画中心
    var params = {
        "[]": {
            "AttrGroup": {
                "showin_selector_flag": 1,
                "@order": "rank-",
                "[]": {
                    "count": 15,
                    "Attr": {
                        "@order": "rank-",
                        "use_flag": 1,
                        "group_id@": "[]/AttrGroup/id"
                    }
                }
            }
        },
        "SeletorPrice[]": {
            "count": 2,
            "SeletorPrice": {
                "@order": "selector_rank-"
            }
        }

    };
    var priceParams = {
        '[]': {
            'SeletorPrice': {
                '@order': 'start+'
            }
        }
    };
    var sortTwoParam = {
        "[]": {
            "AttrGroup": {
                "showin_selector_flag": 1,
                "@order": "rank-",
                "[]": {
                    "count": 2,
                    "Attr": {
                        "@order": "rank-",
                        "use_flag": 1,
                        "group_id@": "[]/AttrGroup/id",
                        "showin_selector_flag": 1
                    }
                }
            }
        },
        "SeletorPrice[]": {
            "count": 2,
            "SeletorPrice": {
                "@order": "selector_rank-"
            }
        }

    };
    send_ajax('post', '/get', params, function callback(res) {
        if (res.code == 200 && res['[]']) {
            var data = res['[]'];
            var temp = [];
            data.forEach(function (element) {
                if (null == element.AttrGroup.cat_id) {
                    temp.push(element);
                }
            });
            data = temp;
            var dataPrice = res['SeletorPrice[]'];
            for (var i = 0; i < data.length; i++) {
                //属性分组前三个
                $('.choose_center').append('<li class="choose_open">\n                    <a href="/xuanhua/' + data[i].AttrGroup.url + '/">\n                        <img src="/asset/image/' + (data[i].AttrGroup.url + '.png') + '" alt="">\n                        <span>' + data[i].AttrGroup.short_name + '</span>\n                        <i class="drop_pic"></i>\n                    </a>\n                </li>\n                ');
            }
            for (var i = 0; i < data.length; i++) {
                //前三个属性分组的悬浮框
                $('.choose_open').eq(i).append('<div class="other_sub_xuanhua_box"></div>');
                for (var j = 0; j < data[i].AttrGroup['[]'].length; j++) {
                    $('.choose_open').eq(i).find(".other_sub_xuanhua_box").append('<a href="/xuanhua/' + data[i].AttrGroup.url + '/' + data[i].AttrGroup['[]'][j].Attr.url + '/">' + data[i].AttrGroup['[]'][j].Attr.value + '</a>');
                }
            }
            //价格属性分组单独写
            $('.choose_center').append('<li class="choose_open">\n               <a href="/xuanhua/jiage">\n                   <img src="/asset/image/jiage.png" alt="">\n                   <span>\u4EF7\u683C</span>\n                   <i class="drop_pic"></i>\n               </a></li>');
            $('.choose_center').append('<ul class="column_box"></ul>');
            send_ajax('post', '/get', sortTwoParam, function callback(res) {
                if(res['[]']){
                    var result = res['[]'];
                    for (var j = 0; j < result.length; j++) {
                        //取数据里的前两个value，放在下边li里
                        for (var i = 0; i < 2; i++) {
                            $('.column_box').append(' <li>\n                                <a href="/xuanhua/' + result[j].AttrGroup.url + '/' + result[j].AttrGroup['[]'][i].Attr.url + '/">\n                                    <img src="/asset/image/' + (result[j].AttrGroup['[]'][i].Attr.url + '.png') + '" alt="">\n                                    <span>' + result[j].AttrGroup['[]'][i].Attr.short_value + '</span>\n                                </a>\n                            </li>');
                        }
                    };
                    for (var i = 0; i < dataPrice.length; i++) {
                        //价格的两个value，放在下边li里
                        $('.column_box').append('<li>\n                <a href="/xuanhua/jiage/' + dataPrice[i].url + '/">\n                    <img src="/asset/image/' + (dataPrice[i].url + '.png') + '" alt="">\n                    <span>' + dataPrice[i].content + '</span>\n                </a></li>');
                    };
                }
                
            });
            send_ajax('post', '/get', priceParams, function callback(res) {
                if (res.code == 200&&res["[]"]) {
                    var data = res["[]"];
                    $('.choose_open').eq(3).append('<div class="other_sub_xuanhua_box"></div>');
                    for (var i = 0; i < data.length; i++) {
                        //价格悬浮框
                        $('.choose_open').eq(3).find('.other_sub_xuanhua_box').append('<a href="/xuanhua/' + data[i].SeletorPrice.url + '/">' + data[i].SeletorPrice.content + '</a>');
                    }
                }
            });
        }
        //选画中心切换
        $('.choose_open').mouseenter(function () {
            //导航
            var index = $(this).index();
            $(this).addClass("c_pic").siblings().removeClass("c_pic");
            $('.other_sub_xuanhua_box').eq(index).addClass("open").siblings().removeClass("open"); //对应的div
        }).mouseleave(function () {
            $(this).removeClass("c_pic");
            $('.other_sub_xuanhua_box').removeClass("open"); //对应的div
        });
    });
    //名家
    var params = {
        'WebPage': {
            'page_sn': 'goods_search'
        },
        'AdPosition': {
            'position_sn': 'aside_artist'
        },
        '[]': {
            'count': 2,
            'Ad': {
                'page_id@': 'WebPage/id',
                'position_id@': 'AdPosition/id'
            }
        }
    };

    send_ajax('post', '/get', params, function callback(res) {
        if (res.code == 200 && res["[]"]) {
            var data = res["[]"];
            for (var i = 0; i < data.length; i++) {
                $('.pic_list').append('<li>\n                    <a href="/' + data[i].Ad.ad_link + '"><img src="' + isImgHttp(data[i].Ad.img) + '" title="\u4E07\u5BCC\u6C38\u98CE\u6C34\u4E66\u6CD5\u5B9A\u5236\uFF01" alt="' + data[i].Ad.img_alt + '"></a>\n                </li>');
            }
        }
    });
    //新品字画
    var params = {
        "[]": {
            "count": 2,
            "GoodsRank": {
                "type": 1
            },
            "Goods": {
                "id@": "[]/GoodsRank/goods_id"
            },
            "GoodsImgs": {
                "goods_id@": "[]/Goods/id",
                "main_flag": 1
            }
        }
    };
    send_ajax('post', '/get', params, function callback(res) {
        if (res.code == 200 && res["[]"]) {
            var data = res["[]"];
            for (var i = 0; i < data.length; i++) {
                if (data[i].Goods) {
                    $('.new').append('<a target="_blank" href="/goods-' + data[i].Goods.id + '.html"><li>\n                    <img src="' + isImgHttp(data[i].GoodsImgs.img) + '" alt="">\n                    <div class="artist_msg">' + data[i].Goods.name + '</div>\n                    <p>\uFFE5' + data[i].Goods.goods_price + '</p>\n                </li></a>');
                }
            }
        }
    });

    //推荐字画
    var params = {
        "[]": {
            "count": 2,
            "GoodsRank": {
                "type": 2
            },
            "Goods": {
                "id@": "[]/GoodsRank/goods_id"
            },
            "GoodsImgs": {
                "goods_id@": "[]/Goods/id",
                "main_flag": 1
            }
        }
    };
    send_ajax('post', '/get', params, function callback(res) {
        if (res.code == 200 && res["[]"]) {
            var data = res["[]"];
            for (var i = 0; i < data.length; i++) {
                if (data[i].Goods) {
                    $('.recom').append('<a target="_blank" href="/goods-' + data[i].Goods.id + '.html"><li>\n                    <img src="' + isImgHttp(data[i].GoodsImgs.img) + '" alt="">\n                    <div class="artist_msg">' + data[i].Goods.name + '</div>\n                    <p>\uFFE5' + data[i].Goods.goods_price + '</p>\n                </li></a>');
                }
            }
        }
    });
    //推荐活动
    var params = {
        '[]': {
            'count': 2,
            'Activity': {
                '@order': 'rank'
            }
        }
    };
    send_ajax('post', '/get', params, function callback(res) {
        if (res.code == 200 && res["[]"]) {
            var data = res["[]"];
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += '<li><a>' + data[i].Activity.title + '</a></li> ';
            };
            $('.activity').html(html);
        }
    });
    //促销字画
    var params = {
        "[]": {
            "count": 2,
            "GoodsRank": {
                "type": 4
            },
            "Goods": {
                "onsale_flag": 1,
                "gift_flag": 0,
                "preview_flag": 0,
                "id@": "[]/GoodsRank/goods_id",
                "promotion_flag": 1,
                "promotion_start%": "2000-01-01 00:00:00,2019-01-01 00:00:00",
                "promotion_end%": "2019-01-01 00:00:00,3000-01-01 00:00:00"
            },
            "GoodsImgs": {
                "goods_id@": "[]/Goods/id",
                "main_flag": 1
            }
        }
    };
    send_ajax('post', '/get', params, function callback(res) {
        if (res.code == 200 && res["[]"]) {
            var data = res["[]"];
            for (var i = 0; i < data.length; i++) {
                if (data[i].Goods) {
                    $('.promotion').append('<a target="_blank" href="/goods-' + data[i].Goods.id + '.html"><li>\n                    <img src="' + isImgHttp(data[i].GoodsImgs.img) + '" alt="">\n                    <div class="artist_msg">' + data[i].Goods.name + '</div>\n                    <p>\uFFE5' + data[i].Goods.goods_price + '</p>\n                </li></a>');
                }
            }
        }
    });

    //推荐名家
    var params = {
        "[]": {
            "count": 2,
            "Artist": {
                "@order": "recommend_rank-"
            }
        }
    };
    send_ajax('post', '/get', params, function callback(res) {
        if (res.code == 200 && res["[]"] != undefined) {
            var data = res["[]"];
            for (var i = 0; i < data.length; i++) {
                $('.artist_pic').append('<a target="_blank" href="/mj/zuozhe-' + data[i].Artist.id + '/"><li>\n                    <img src="' + isImgHttp(data[i].Artist.portrait) + '" alt="">\n                    <div class="artist_msg">' + data[i].Artist.name + '</div>\n                </li></a>');
            }
        }
    });
});